package com.liveinstars.post.business.repository;

import com.liveinstars.post.business.model.entity.PostLike;
import com.liveinstars.post.business.model.enums.PostStatus;
import com.liveinstars.post.business.repository.base.BaseRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.lang.NonNull;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Set;


/**
 * Post category repository.
 *
 * @author johnniang
 * @author ryanwang
 * @date 2019-03-19
 */
public interface PostLikeRepository extends BaseRepository<PostLike, Integer> {

    /**
     * Finds all category ids by post id
     *
     * @param postId post id must not be null
     * @return a list of category id
     */
    @NonNull
    @Query("select postLike.userId from PostLike postLike where postLike.postId = ?1")
    Set<Integer> findAllUserIdsByPostId(@NonNull Integer postId);

    /**
     * Finds all post ids by category id.
     *
     * @param userId category id must not be null
     * @return a set of post id
     */
    @NonNull
    @Query("select postLike.postId from PostLike postLike where postLike.userId = ?1")
    Set<Integer> findAllPostIdsByUserId(@NonNull Integer userId);

    /**
     * Finds all post ids by category id and post status.
     *
     * @param userId category id must not be null
     * @param status     post status must not be null
     * @return a set of post id
     */
    @NonNull
    @Query("select postLike.postId from PostLike postLike, Post post where postLike.userId = ?1 and post.id = postLike.postId and post.status = ?2")
    Set<Integer> findAllPostIdsByUserId(@NonNull Integer userId, @NonNull PostStatus status);

    /**
     * Finds all post userids by post id in.
     *
     * @param postIds post id collection must not be null
     * @return a list of post category
     */
    @NonNull
    List<PostLike> findAllByPostIdIn(@NonNull Collection<Integer> postIds);

    /**
     * Finds all post userids by post id.
     *
     * @param postId post id must not be null
     * @return a list of post category
     */
    @NonNull
    List<PostLike> findAllByPostId(@NonNull Integer postId);

    /**
     * Finds all post userids by category id.
     *
     * @param userId category id must not be null
     * @return a list of post category
     */
    @NonNull
    List<PostLike> findAllByUserId(@NonNull Integer userId);

    @NonNull
    List<PostLike> findAllByUserIdAndPostIdAndType(@NonNull Integer userId, @NonNull Integer postId, Integer type);
    /**
     * Deletes post userids by post id.
     *
     * @param postId post id must not be null
     * @return a list of post category deleted
     */
    @NonNull
    List<PostLike> deleteByPostId(@NonNull Integer postId);



    /**
     * Deletes post userids by category id.
     *
     * @param userId category id must not be null
     * @return a list of post category deleted
     */
    @NonNull
    List<PostLike> deleteByUserId(@NonNull Integer userId);


    @Modifying
    @Transactional
    void deleteByPostIdAndUserIdAndType(@NonNull Integer postId, @NonNull Integer userId, Integer type);


    //查询点赞的人的文章是否有被被点赞的人点赞过的
    @Query(nativeQuery = true, value = "SELECT count(*) FROM posts as a, post_like as b where a.id = b.post_id and a.user_id = ?2 and b.user_id = ?1 and b.type = 0")
    int matchLikeRelation(Integer ownerId, Integer userId);
}
